package com.wswy.wzcx.ui.module.feedback

import android.arch.lifecycle.Observer
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.che.libcommon.ui.adapter.SampleRecyclerViewAdapter
import com.che.libcommon.ui.loading.ILoadingView
import com.facebook.common.util.UriUtil
import com.facebook.drawee.view.SimpleDraweeView
import com.google.android.flexbox.FlexboxLayout
import com.stfalcon.frescoimageviewer.ImageViewer
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.*
import kotlinx.android.synthetic.main.fragment_my_feedback.*
import java.io.File

/**
 * 我的反馈记录
 */
class MyFeedbackFragment : CBaseLoadingFragment() {

    override fun getConfigure(): ILoadingView.LoadingConfigure {
        return object : ILoadingView.SampleLoadingConfigure(){
            override fun getOnRetryClickListener(): View.OnClickListener {
                return View.OnClickListener {



                }
            }
        }
    }

    override fun getLoadingFrameId(): Int {
        return R.id.fl_content
    }

    override fun getFragmentTitle(): String {
        return "反馈记录"
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_my_feedback
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        recycler_view.layoutManager = LinearLayoutManager(requireContext())

        val adapter =  MyFeedbackAdapter()

        recycler_view.adapter = adapter

        val feedbackViewModel = provideViewModel(FeedbackViewModel::class.java)


        feedbackViewModel.getMyFeedbackListLiveData().observe(this, Observer {


            it?.run {


                if (inLoading()){
                    showLoading(true)
                }else if (isOk()){

                    showData()

                    adapter.setNewData(data)
                    adapter.notifyDataSetChanged()

                }else{
                    showError(null as Throwable)
                }



            }

        })

        feedbackViewModel.myFeedback()
    }



    private class MyFeedbackAdapter : SampleRecyclerViewAdapter<FBaseVO,RecyclerView.ViewHolder>(){

        private companion object {
            private const val TYPE_QUESTION = 1
            private const val TYPE_ANSWER = 2
        }

        override fun getItemViewType(position: Int): Int {
            return if (datas.get(position) is FQuestionVO) TYPE_QUESTION else TYPE_ANSWER
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

           return when(viewType){
                TYPE_QUESTION -> QuestionViewHolder(loadLayout(parent,R.layout.item_myfeedback_question))
                TYPE_ANSWER -> ReplyViewHolder(loadLayout(parent,R.layout.item_myfeedback_reply))
               else -> QuestionViewHolder(loadLayout(parent,R.layout.item_myfeedback_question))
            }

        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, data: FBaseVO?) {

            when(holder){
                is QuestionViewHolder -> {
                    (data as? FQuestionVO)?.run {
                        holder.tvType.text = type
                        holder.tvTime.text = time
                        holder.tvQuestion.text = content

                        showImages(holder.flImages,images)

                    }
                }

                is ReplyViewHolder -> {
                    (data as? FReplyVO)?.run {

                        holder.tvTime.text = time
                        holder.tvReplyContent.text = content

                        showImages(holder.flImages,images)
                    }
                }
            }

        }



        private fun showImages(flImages:FlexboxLayout,imgs: List<String>?){
            if (imgs.isNullOrEmpty()){
                flImages.gone()


            }else{
                flImages.removeAllViewsInLayout()
                flImages.visible()

                val clickPhoto = View.OnClickListener {

                    val pos = (it.getTag(R.id.item_position) as? Int)?:0

                    ImageViewer.Builder(it.context,imgs)
                            .setStartPosition(pos)
                            .show()

                }


                imgs.forEachIndexed { index, s ->
                    flImages.addView(createImage(flImages.context,s).apply {
                        setTag(R.id.item_position,index)
                        setOnClickListener(clickPhoto)
                    },ViewGroup.MarginLayoutParams(SizeUtils.dp2px(64f),SizeUtils.dp2px(64f)).apply {
                        rightMargin = SizeUtils.dp2px(4f)
                    })
                }

            }

        }
        private fun createImage(context:Context,url:String):ImageView{
            return SimpleDraweeView(context).apply {

                setImageURI(url)
                hierarchy.setPlaceholderImage(R.color.dark_bg_color)
            }
        }
    }


    private class QuestionViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView){

        val tvType : TextView = itemView.findViewById(R.id.tv_type)
        val tvTime:TextView = itemView.findViewById(R.id.tv_q_time)
        val tvQuestion:TextView = itemView.findViewById(R.id.tv_q_content)
        val flImages : FlexboxLayout = itemView.findViewById(R.id.fl_imgs_container)
    }

    private class ReplyViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView){
        val tvTime:TextView = itemView.findViewById(R.id.tc_reply_time)
        val tvReplyContent:TextView = itemView.findViewById(R.id.tv_reply_content)
        val flImages : FlexboxLayout = itemView.findViewById(R.id.fl_imgs_container)
    }


}